<script type="text/javascript">        
// create an array that holds all the selected applications
<? 
$a=1; $app_str = ''; 
foreach ($product_applications as $prod_app){
	$app_str .= "'".$prod_app."'";
    if ($a<count($product_applications)) $app_str .= ',';
    $a++;
} 
?>
var selected_apps = new Array(<?=$app_str?>);       

function Load_Application_List(){
	
		loading_queue++;
        $.post("<?=site_url("/admin/products/get_application_list")?>", {},
            function(data){
                $('#apps_container').html(data);
                Check_Applications();
				  loading_queue--;
            }, "html"
        );
        
}

//loops through the applications and checks the selected applications
function Check_Applications(){

    for (i=0;i<selected_apps.length;i++)
    {
         $('#apps_container').find('input:checkbox[value='+selected_apps[i]+']').attr('checked', true);
	} 
    $.each($('#apps_container input:checkbox'), function(){SetCheckboxClass(this)});

}       

$(document).ready(function(){
                               
    Load_Application_List();   

    // create new application
    $("#create_new_app").click(function(){
        var name = $.trim($('#new_app_name').val());
        var image = $('#app_img_input').val();
                
        $.post("<?=site_url("/admin/applications/add")?>", 
            { name: name, image: image, json: true},
            function(data){    
                if(data.success==true){
                    var html_str = data.response;
                    selected_apps.push(data.item.id);
                    Load_Application_List();
                    $('#new_app_name').val("");
                    $('#app_img_input').val("");
                    $('#app_img').attr("src","");
                } else {
                    var html_str = data.response;
                }
                $('#new_app_response').html(html_str).hide().fadeIn();

            }, "json"
        );
        return false;
    }); 
    //disable form submition when pressing enter on text field
    $("#new_app_name").bind("keypress", function(e,a) {
        if (e.keyCode == 13) {
            $("#create_new_app").click();
            return false;
        }
    });

    // when a checkbox is clicked, the selected_apps array is updated
    $('#apps_container input:checkbox').live('click', function(){

        v = $(this).val();
        if ($(this).is(':checked')) { 
            selected_apps.push(v);
        } else {            
            for (var i=0; i<selected_apps.length; i++) {
                if (selected_apps[i] == v) selected_apps.splice(i, 1);
            }
        }
        SetCheckboxClass(this);                                                       
        
    });  

});        

</script>


<div class="live_search_msg"></div>

<div id="apps_container" class="content_block"  style="width:100%; overflow:auto">
    loading applications...
</div>


<div class="content_block" style="overflow:auto">

                                                                               
    <div class="float_block" style="width:200px">
        <input type="text" id="new_app_name" maxlength="200" />
    </div>

    <div class="float_block ImageManager_Widget" style="width:auto !important">
        <img id="app_img" style="display:block" />
        <a class="open_image_manager button" href="#">SELECT IMAGE</a>
        <input type="hidden" id="app_img_input" class="img_path" />    
    </div>

    <a id="create_new_app" href="#" class="button">ADD NEW</a>


    <div style="clear:both" id="new_app_response"></div>

</div>
